package com.hoongs.v1.controller;

import com.hoongs.v1.service.FileService;
import com.hoongs.v1.utils.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.FileNotFoundException;
import java.io.IOException;

@RestController
@RequestMapping("/file")
@Validated
public class FileController {

    @Resource
    private FileService fileService;

    /**
     * 1.文件保存在服务器，url地址保存在数据库
     * 上传成功之后返回成功保存的url地址
     */
    @PostMapping("/fileUpload")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result fileUpload(@RequestParam("files") MultipartFile[] files) throws IOException {
//        if(!file.isEmpty()){
//            return fileService.fileUpload(file);
//        }else
        if (files == null || files.length > 0) {
            return fileService.filesUpload(files);
        } else {
            return Result.opError("上传文件不存在！");
        }
    }
}
